package models;
import java.util.*;
import javax.persistence.*;
import play.data.format.*;
import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class Participante {

    @Id
    public Integer numParticipante;
    
    @ManyToOne
    @JoinColumn(name="numPersona")
    public Persona persona;
    
    @ManyToOne
    @JoinColumn(name="codSorteo")
    public Sorteo sorteo;
//    
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date fechaInscripcion;

    public Integer indicadorSorteado;

    public Integer indicadorGanador;
    
    public static Participante findById(Integer numParticipante) {
        return JPA.em().find(Participante.class, numParticipante);
    }
    
    public void update(Integer numParticipante) {
        this.numParticipante = numParticipante;
        JPA.em().merge(this);
    }
    
    public void save() {
        JPA.em().persist(this);
    }
    
    public void delete() {
        JPA.em().remove(this);
    }
    
    
   public static Page page(int codSorteo) {
        @SuppressWarnings("unchecked")
				List<Participante> data = JPA.em()
            .createQuery("from Participante p where p.sorteo.codSorteo like ?")
            .setParameter(1, codSorteo)
            .getResultList();
        

        Long total=Long.parseLong(data.size()+"");
        return new Page(data, total, 1, 10);
    }
   
   public static Page page() {
        @SuppressWarnings("unchecked")
	List<Participante> data = JPA.em()
            .createQuery("from Participante p ")
            .getResultList();

        Long total=Long.parseLong(data.size()+"");
        return new Page(data, total, 1, 10);
    }
   
     public static class Page {
        
        private final int pageSize;
        private final long totalRowCount;
        private final int pageIndex;
        private final List<Participante> list;
        
        public Page(List<Participante> data, long total, int page, int pageSize) {
            this.list = data;
            this.totalRowCount = total;
            this.pageIndex = page;
            this.pageSize = pageSize;
        }
        
        public long getTotalRowCount() {
            return totalRowCount;
        }
        
        public int getPageIndex() {
            return pageIndex;
        }
        
        public List<Participante> getList() {
            return list;
        }
        
        public boolean hasPrev() {
            return pageIndex > 1;
        }
        
        public boolean hasNext() {
            return (totalRowCount/pageSize) >= pageIndex;
        }
        
        public String getDisplayXtoYofZ() {
            int start = ((pageIndex - 1) * pageSize + 1);
            int end = start + Math.min(pageSize, list.size()) - 1;
            return start + " al " + end + " de " + totalRowCount;
        }
        
    }
}
